=begin rdoc
关于博客的基本操作
=end
class Manager::Blog::BlogsController < Admin::WmsApplicationController
  include Admin::Users::SessionsHelper

  before_action :set_post, only: [:show, :destroy, :edit, :update]
  before_action :blog_sorts_params, only: [:edit, :index, :new]

  #数据操作权限检测
  #这是简便方式，必须遵守RESTful接口设计
  #默认情况下会以控制器名称映射模型类名称，如果两则不同则需要单独声明。
  load_and_authorize_resource :post
  

  def initialize
    super
  end

  #博客列表
  def index
    @posts = Post.all
  end

  def new
    @post = Post.new

  end

  def create
    @post = Post.new(post_params)
    @post.author = cus_current_user_email
    @post.admin_user = current_user

    if @post.save
      redirect_to post_url(@post)
    else
      redirect_to new_manager_blog_blog_path
    end
  end

  def show
    redirect_to post_path(@post)
  end

  def update
    store_location
    if @post.update(update_params)
      redirect_to post_path(@post)
    else
      redirect_back_or edit_manager_blog_blog_path(@post)
    end
  end

  def destroy
    store_location

    respond_to do |format|
      if @post.destroy
        format.js    #直接返回js脚本，默认为destroy.js.erb文件
      else
        format.json { render json: {error: '删除失败！'}, status: 500 }
      end
    end

  end

  def edit
    @status = __method__
    respond_to do |format|
      format.html { render action: :new}
    end
  end





  private
  # Use callbacks to share common setup or constraints between actions.
  def set_post
    @post = Post.find(params[:id])
  end

  def blog_sorts_params
    @blog_sorts = BlogSort.all
  end
  # Never trust parameters from the scary internet, only allow the white list through.
  def post_params
    params.require(:post).permit(:title, :content, :blog_sort_id)
  end

  def update_params
    params.require(:post).permit(:content, :blog_sort_id,:id)
  end
  def create_params
    params.require(:post).permit(:id, :title, :content, :blog_sort_id)
  end
end
